'use strict';

angular.module('app')
    .controller('backCashDetailController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '返现详情';
            $scope.param = { };
            $scope.loading = false;

            $scope.search = function () {
                $scope.param.userId = $state.params.id;
                $scope.loading = false;
                $.ajax({
                    url : $scope.app.project_name+'/statsData/backCash/detail',
                    data: $scope.param
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.param.startTime=$("#startTime").val();
                $scope.param.endTime=$("#endTime").val();
                $scope.loading = true;
                $scope.param.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/cooper/read/list',
                    data: $scope.param
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            //清空
            $scope.clearSearch = function() {
                $scope.param = { };
                $("#startTime").val("");
                $("#endTime").val("");
                $scope.search();
            }
            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum=page;
                $scope.search();
            };
            /**
             * 展现详情
             */
            $scope.preview = function(id){
                $.ajax({
                    url : $scope.app.project_name+'',
                    data: {'userId': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
                $scope.firstApply = true;
            }


            $scope.showBackCash = function () {
                $.ajax({
                    url : $scope.app.project_name+'/statsData/backCash/backCashInfo',
                    data: {'userId': $state.params.id}
                }).then(function(result) {
                    if (result.httpCode == 200) {
                        $scope.backCashInfo ={};
                        $scope.backCashInfo.userId = $state.params.id;
                        $scope.backCashInfo.Username = result.data.Username;
                        $scope.backCashInfo.cash = result.data.cash;
                        $scope.backCashInfo.backType = "";
                        $scope.backCashInfo.nowcash ="";
                        $('#backCash').modal('show',true);
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.baskCash = function () {
                if(!$scope.Validate($("#saveBackCash"))){
                    return;
                }
                $.ajax({
                    url : $scope.app.project_name+'/statsData/backCash/backCash',
                    data:$scope.backCashInfo
                }).then(function(result) {
                    if (result.data=="") {
                        $("#backCash").modal('hide');
                        $scope.alertModal({'content':'操作成功'});
                        $scope.search();
                    } else {
                        $("#backCash").modal('hide');
                        $scope.alertModal({'content':result.data});
                    }
                    $scope.$apply();
                });
            }

            $scope.clearNoNum = function(obj,attr){
                //先把非数字的都替换掉，除了数字和.
                obj[attr] = obj[attr].replace(/[^\d.]/g,"");
                //必须保证第一个为数字而不是.和0
                obj[attr] = obj[attr].replace(/^\./g,"");
                // obj[attr] = obj[attr].replace(/^0/g,"");
                //保证只有出现一个.而没有多个.
                obj[attr] = obj[attr].replace(/\.{2,}/g,"");
                //保证.只出现一次，而不能出现两次以上
                obj[attr] =   obj[attr].replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            }

            $scope.back=function () {
                $state.go("main.statsData.companyLaunch.list");
            }
            $scope.search();
        } ]);